﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace PCRemotePCver
{
    public partial class MainForm : Form
    {
        private static bool IsStarted = false;
        private static Thread ServerListenning = null;
        private static bool hotspotConnect = false;
        public MainForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            StartServer();
        }

        public void StartServer()
        {
            if (!IsStarted)
            {
                AsynchronousSocketListener.IsClosed = false;
                ServerListenning = new Thread(new ThreadStart(RunServer));
                ServerListenning.Start();
                Thread.Sleep(1000);
                IsStarted = true;
                button1.Text = "Stop Server";
                tbStatus.Text = "Server Running...";
            }
            else
            {
                IsStarted = false;
                AsynchronousSocketListener.IsClosed = true;
                AsynchronousSocketListener.listener.Close();
                AsynchronousSocketListener.client.Close();
                button1.Text = "Start Server";
                tbStatus.Text = "Server Stoped";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartServer();
        }

        private void RunServer()
        {
            AsynchronousSocketListener.StartListening();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //if (!IsAdmin())
            //{
            //    RestartElevated();
            //}
            GetNetworkInfo();
        }
        private void GetNetworkInfo()
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            ipListComboBox.Items.Clear();
            for (int i = 0; i < ipHostInfo.AddressList.Length; i++)
            {
                if (!ipHostInfo.AddressList[i].ToString().StartsWith("169"))
                {
                    ipListComboBox.Items.Add(ipHostInfo.AddressList[i].ToString());
                }
            }
            ipListComboBox.SelectedIndex = 0;
            tbComputerName.Text = Dns.GetHostName();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (ServerListenning != null)
                ServerListenning.Abort();
            
            button1.Text = "Start Server";
            tbStatus.Text = "Server Stoped";

            //Shutdown the last session
            if (IsStarted)
            {
                AsynchronousSocketListener.IsClosed = true;
                AsynchronousSocketListener.listener.Close();                
            }
            
            //Start new session
            AsynchronousSocketListener.IsClosed = false;
            ServerListenning = new Thread(new ThreadStart(RunServer));
            ServerListenning.Start();
            Thread.Sleep(1000);
            IsStarted = true;
            button1.Text = "Stop Server";
            tbStatus.Text = "Server Running...";
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string ssid = textBox1.Text, key = textBox2.Text;
            if (!hotspotConnect)
            {
                if (textBox1.Text == null || textBox1.Text == "")
                {
                    MessageBox.Show("SSID cannot be left blank !",
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    if (textBox2.Text == null || textBox2.Text == "")
                    {
                        MessageBox.Show("Key value cannot be left blank !",
                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (key.Length >= 6)
                        {
                            Zedfi_Hotspot(ssid, key, true);
                            textBox1.Enabled = false;
                            textBox2.Enabled = false;
                            button3.Text = "Stop";
                            hotspotConnect = true;
                        }
                        else
                        {
                            MessageBox.Show("Key should be more then or Equal to 6 Characters !",
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            else
            {
                Zedfi_Hotspot(null, null, false);
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                button3.Text = "Start";
                hotspotConnect = false;
            }
        }
        private void Zedfi_Hotspot(string ssid, string key,bool status)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.CreateNoWindow = true;
            processStartInfo.UseShellExecute = false;
            Process process = Process.Start(processStartInfo);

            if (process != null)
            {
                if (status)
                {
                    process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow ssid=" + ssid + " key=" + key);
                    process.StandardInput.WriteLine("netsh wlan start hosted network");
                    process.StandardInput.Close();
                }
                else
                {
                    process.StandardInput.WriteLine("netsh wlan stop hostednetwork");
                    process.StandardInput.Close();
                }
            }
            GetNetworkInfo();
        }
        public static bool IsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal p = new WindowsPrincipal(id);
            return p.IsInRole(WindowsBuiltInRole.Administrator);
        }
        public void RestartElevated()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.CreateNoWindow = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
            startInfo.Verb = "runas";
            try
            {
                Process p = Process.Start(startInfo);
            }
            catch
            {

            }

            System.Windows.Forms.Application.Exit();
        }
        public void ChangePortText(int Port)
        {
            lbPortListen.BeginInvoke((Action)(() =>
                {
                    lbPortListen.Text = "Port: " + Port;
                }));
        }
    }
}
